<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-02
 * Time: 下午 5:33
 * 资源类
 */
namespace app\api\controller;
use think\Controller;
use think\Db;
class Source extends Controller{
    //添加资源
    public function addSource($data){
        $res = Db::name('source')->getLastInsID($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }
    //我的资源列表
//    public function sourceList($member_id){
//        $data = Db::name('source')->where('member_id',$member_id)->select();
//        if(!empty($data)){
//            foreach($data as $k=>$v){
//                $data[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
//            }
//        }
//        return $data;
//    }
    //删除资源
    public function deleteSource($source_id){
        $res = Db::name('source')->delete($source_id);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //修改资源
    public function saveSource($source_id,$data){
        $res = Db::name('source')->where('source_id',$source_id)->update($data);
        if($res === false){
            return false;
        }else{
            return true;
        }
    }
    //群资源列表
    public function qunSourceList($qun_id,$page,$count){
        $data = Db::name('qun_source')
            ->join('source','snake_source.source_id = snake_qun_source.source_id')
            ->where('snake_qun_source.qun_id',$qun_id)
            ->limit($count)
            ->page($page)
            ->select();
        return $data;
    }
    //会员所在所有群资源列表
    public function qunSourceListAll($qun_id,$keyword){
        $data = Db::name('qun_source')
            ->join('source','snake_source.source_id = snake_qun_source.source_id')
            ->where('snake_qun_source.qun_id in ('.$qun_id.')')
            ->where('snake_source.detail like "%'.$keyword.'%"')
            ->select();
        return $data;
    }
    //会员所在所有群资源列表
    public function qunSourceListAllType($qun_id,$type){
        $data = Db::name('qun_source')
            ->join('source','snake_source.source_id = snake_qun_source.source_id')
            ->where('snake_qun_source.qun_id in ('.$qun_id.')')
            ->where('snake_source.type = '.$type)
            ->select();
        return $data;
    }
    //资源详情
    public function sourceDetail($source_id){
        $data = Db::name('source')->where('source_id',$source_id)->find();
        return $data;
    }
    //会员资源列表
    public function getMemberSource($member_id,$page,$count,$type){
        $map = array();
        if($type != 3){
            $map['type'] = $type;
        }
       return  Db::name('source')->where('member_id',$member_id)->limit($count)->page($page)->order('addtime desc')->where($map)->select();
    }
    //会员对群可见的资源
    public function getQunMemberSource($qun_id,$member_id){
        return  Db::name('qun_source')
            ->join('source','snake_source.source_id = snake_qun_source.source_id')
            ->where('snake_qun_source.member_id',$member_id)
            ->where('snake_qun_source.qun_id',$qun_id)
            ->select();
    }
    //查询某个会员对某个会员可见的资源
    public function friendSeeMySource($member_id,$friend_id){
        $data = Db::name('source')->where('member_id',$member_id)->select();
        foreach($data as $k=>$v){
            $str = explode(',',$v['power']);
            if($v['is_type'] == 1){
                $arr[] = $v;
            }elseif($v['is_type'] == 2){
                if(!in_array($friend_id,$str)){
                    $arr[] = $v;
                }
            }else{
                if(in_array($friend_id,$str)){
                    $arr[] = $v;
                }
            }
        }
        return $arr;
    }
    //查询某个会员的所有好友的自己可见的资源
    public function friendSeeMySourceAll($member_id,$friend_id,$keyword){
        $data = Db::name('source')
            ->where('member_id in ('.$friend_id.')')
            ->where('detail like "%'.$keyword.'%"')
            ->select();
        foreach($data as $k=>$v){
            $str = explode(',',$v['power']);
            if($v['is_type'] == 1){
                $arr[] = $v;
            }elseif($v['is_type'] == 2){
                if(!in_array($member_id,$str)){
                    $arr[] = $v;
                }
            }else{
                if(in_array($member_id,$str)){
                    $arr[] = $v;
                }
            }
        }
        return $arr;
    }
    //查询某个会员的所有好友的自己可见的资源
    public function friendSeeMySourceAllType($member_id,$friend_id,$type){
        $data = Db::name('source')
            ->where('member_id in ('.$friend_id.')')
            ->where('type',$type)
            ->order('addtime desc')
            ->select();
        foreach($data as $k=>$v){
            $str = explode(',',$v['power']);
            if($v['is_type'] == 1){
                $arr[] = $v;
            }elseif($v['is_type'] == 2){
                if(!in_array($member_id,$str)){
                    $arr[] = $v;
                }
            }else{
                if(in_array($member_id,$str)){
                    $arr[] = $v;
                }
            }
        }
        return $arr;
    }
    //判断资源有没有收藏
    public function checkCollectSource($source_id,$member_id){
        $collect = Db::name('collect')->where('member_id',$member_id)->select();
        if(empty($collect)){
            return 0;
        }
        foreach($collect as $k=>$v){
            $arr[] = $v['source_id'];
        }
        if(in_array($source_id,$arr)){
            return 1;
        }
        return 0;
    }
}